package com.travel.server.manager.impl.command.customer;

import com.travel.common.CommonConstant;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.customer.AddCustomerInfoRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.CustomerDal;
import com.travel.server.manager.impl.dal.CustomerTagDal;
import com.travel.server.manager.impl.dal.MemberGradeDal;
import com.travel.server.manager.impl.dal.TagDal;
import com.travel.server.manager.impl.sal.CustomerSal;
import com.travel.server.manager.model.CustomerInfo;
import com.travel.server.manager.model.CustomerTagInfo;
import com.travel.server.manager.model.MemberGradeInfo;
import com.travel.server.manager.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/9 18:06
 * @Desc:
 */
@Component
@Slf4j
public class AddCustomerInfoCommand extends AbstractCommand<AddCustomerInfoRequest, EmptyResponse> {

    @Autowired
    private CustomerDal customerDal;

    @Autowired
    private MemberGradeDal memberGradeDal;

    @Autowired
    private TagDal tagDal;

    @Autowired
    private CustomerTagDal customerTagDal;

    @Override
    protected void checkInput(AddCustomerInfoRequest request) {

    }

    @Override
    public EmptyResponse handle(AddCustomerInfoRequest request) {
        UserInfo userInfo = getUserInfo();
        Date now = new Date();

        CustomerInfo customerInfo = new CustomerInfo();
        if(request.getTagId() != null) {
            tagDal.selectById(request.getTagId());
        }
        customerInfo.setName(request.getName());
        customerInfo.setMobile(request.getMobile());
        customerInfo.setWechatNick(request.getWechatNick());
        customerInfo.setWechatNick(request.getNick());
        customerInfo.setMail(request.getMail());
        customerInfo.setLiveCountry(request.getLiveCountry());
        customerInfo.setLiveProvince(request.getLiveProvince());
        customerInfo.setLiveCity(request.getLiveCity());
        customerInfo.setLiveArea(request.getLiveArea());
        customerInfo.setLiveAddress(request.getLiveAddress());
        customerInfo.setSex(request.getSex());
        customerInfo.setGarmentSize(request.getGarmentSize());
        customerInfo.setNote(request.getNote());
        if(request.getMemberGradeId() != null){
            MemberGradeInfo memberGradeInfo = memberGradeDal.selectById(request.getMemberGradeId());
            customerInfo.setMemberGradeId(request.getMemberGradeId());
            customerInfo.setIsMember(CustomerSal.isMember(memberGradeInfo));
        }
        customerInfo.setSchool(request.getSchool());
        customerInfo.setMajor(request.getMajor());
        if(StringUtils.isNotBlank(request.getBirthDate())){
            customerInfo.setBirthDate(DateUtil.strToDate(request.getBirthDate(), DateUtil.YYYY_MM_DD));
        }
        if(StringUtils.isNotBlank(request.getGraduateDate())){
            customerInfo.setGraduateDate(DateUtil.strToDate(request.getGraduateDate(), DateUtil.YYYY_MM_DD));
        }
        customerInfo.setIdentity(request.getIdentity());
        customerInfo.setWorkIndustry(request.getWorkIndustry());
        customerInfo.setUpdateBy(userInfo.getName());
        customerInfo.setUpdateTime(now);
        customerInfo.setCreateBy(userInfo.getName());
        customerInfo.setCreateTime(now);
        customerInfo.setState(CommonConstant.YES);
        Integer customerId = customerDal.insertOrUpdate(customerInfo);
        if(request.getTagId() != null){
            insertTag(customerId, request.getTagId(), userInfo);
        }

        return new EmptyResponse();
    }

    private void insertTag(Integer customerId, Integer tagId, UserInfo userInfo){
        Date now = new Date();

        CustomerTagInfo customerTagInfo = new CustomerTagInfo();
        customerTagInfo.setCustomerId(customerId);
        customerTagInfo.setTagId(tagId);
        customerTagInfo.setState(CommonConstant.YES);
        customerTagInfo.setCreateBy(userInfo.getName());
        customerTagInfo.setCreateTime(now);
        customerTagInfo.setCreateBy(userInfo.getName());
        customerTagInfo.setUpdateBy(userInfo.getName());
        customerTagInfo.setUpdateTime(now);

        customerTagDal.insertOrUpdate(customerTagInfo);
    }
}
